home *** CD-ROM | disk | FTP | other *** search
/ Scene Storm / Scene Storm - Volume 1.iso / coding / c / crmscrunch / crmscrunch.c < prev    next >
C/C++ Source or Header  |  1995-12-29  |  3KB  |  115 lines

  1. /*
  2.  crmscrunch V1.0 by Michael Mutschler
  3.  
  4. This is just an example program of how to use the CrM.library from C.
  5. All it does is crunch a file in LZH & sample mode.
  6.  
  7. note: you have to link with crm.lib!
  8.  
  9. History:
  10. V1.0 - first release
  11. */
  12.  
  13. #include <libraries/crm.h>
  14. #include <libraries/dos.h>
  15. #include <dos/dos.h>
  16. #include    <stdio.h>
  17. #include <stdlib.h>
  18. #include <clib/crm_protos.h>
  19.  
  20. #define ERR_template 20
  21. #define ERR_nolib    19
  22. #define ERR_struct   18
  23. #define ERR_lock        17
  24. #define ERR_NofibMem    16
  25. #define ERR_Nofmem    15
  26. #define ERR_read        14
  27. #define ERR_crunchfail 13
  28. #define ERR_write        12
  29. #define ERR_file        11
  30.  
  31. struct cmCrunchStruct *crunchstruct;
  32. struct Library *CrMBase;
  33. BPTR    handle;
  34. BPTR    lock;
  35. struct FileInfoBlock *fib;
  36. ULONG    flen;
  37. APTR    fmem;
  38. ULONG len, newlen;
  39. struct DataHeader dataheader;
  40. char *version = "$VER: crmscrunch V1.0 by Michael Mutschler";
  41.  
  42. void end(int err);
  43.  
  44. main(int argc, char** argv)
  45. {
  46.     if (argc!=3) end(ERR_template);
  47.  
  48. /* open the library */
  49.     if (!(CrMBase = OpenLibrary("CrM.library",0))) end(ERR_nolib);
  50.  
  51. /* now allocate the crunchstruct.
  52.  * cmF_Sample means use sample mode
  53.  * cmF_Overlay means to use the same buffer for in- and output
  54.  */
  55.     if (!(crunchstruct =
  56.          cmAllocCrunchStruct(CMCS_Algo,cm_LZH | cmF_Sample | cmF_Overlay,TAG_DONE)))
  57.            end(ERR_struct);
  58. /* get the input-file */
  59.     if (!(lock = Lock(argv[1],ACCESS_READ))) end(ERR_lock);
  60.     if (!(fib = malloc(sizeof(struct FileInfoBlock)))) end(ERR_NofibMem);
  61.     Examine(lock, fib);
  62.     flen = fib->fib_Size;
  63.     if (!(fmem = malloc(flen))) end(ERR_Nofmem);
  64.     UnLock(lock);
  65.     lock = NULL;
  66.     handle = Open(argv[1], MODE_OLDFILE);
  67.     len = Read(handle, fmem, flen);
  68.     if (len != flen) end(ERR_read);
  69.     Close(handle);
  70.     handle = NULL;
  71.  
  72. /* now I have the file. Just write the stuff in the crunchstruct: */
  73.     crunchstruct->cmcr_Src    = crunchstruct->cmcr_Dest    = fmem;
  74.     crunchstruct->cmcr_SrcLen = crunchstruct->cmcr_DestLen = flen;
  75.     crunchstruct->cmcr_DataHdr = &dataheader;
  76.  
  77. /* OK, now I can crunch the buffer */
  78.     if (!(newlen = cmCrunchData(crunchstruct))) end(ERR_crunchfail);
  79.  
  80. /* At last, save it */
  81.     if (!(handle = Open(argv[2], MODE_NEWFILE))) end(ERR_file);
  82.     len = Write(handle, &dataheader, sizeof(struct DataHeader));
  83.     if (len != sizeof(struct DataHeader)) end(ERR_write);
  84.     len = Write(handle, fmem, newlen);
  85.     if (len != newlen) end(ERR_write);
  86.  
  87. /* now a little information of our work */
  88.     printf("oldfile: %s  length: %ld\n",argv[1],flen);
  89.     printf("newfile: %s  length: %ld\n",argv[2],newlen);
  90.     end(0);
  91. }
  92.  
  93. void end(int err)
  94. {
  95. /* detailed error report: */
  96.     if (err == ERR_write)        printf("Error while writing!\n");
  97.     if (err == ERR_file)            printf("Dest File open failed!\n");
  98.     if (err == ERR_crunchfail)    printf("crunching failed!\n");
  99.     if (err == ERR_read)            printf("Error while reading!\n");
  100.     if (err == ERR_Nofmem)        printf("No Mem!\n");
  101.     if (err == ERR_template)    printf("Usage: crmscrunch <infile> <outfile>\n");
  102.     if (err == ERR_nolib)        printf("couldn't open Library!\n");
  103.     if (err == ERR_struct)        printf("couldn't allocate struct!\n");
  104.     if (err == ERR_lock)            printf("file not found!\n");
  105.     if (err == ERR_NofibMem)    printf("No Mem!\n");
  106.  
  107.     if (handle) Close(handle);
  108.     if (fmem) free(fmem);
  109.     if (fib) free(fib);
  110.     if (lock) UnLock(lock);
  111.     if (crunchstruct) cmFreeCrunchStruct(crunchstruct);
  112.     if (CrMBase) CloseLibrary(CrMBase);
  113.     exit(err);
  114. }
  115.